﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Demo.DataModel;

namespace Demo.WebApp
{

    #region interfaces
    public interface ICustomerFilter //: IPagingController
    {
        string SearchText { get; set; }
    }

    public interface ICustomerList : ICustomerFilter
    {
        IList<Customer> SelectCustomerList();
        event EventHandler OnCustomerListChanged;
        void SelectCustomer(string key);
        void NewCustomer();
    }

    public interface ICustomerEdit
    {
        void Reset();
        event EventHandler OnCustomerChanged;

        Customer SelectCustomer();
        void UpdateCustomer(Customer Customer);
        void DeleteCustomer(Customer Customer);
        void InsertCustomer(Customer Customer);
    }
    #endregion

    public class CustomerController : ICustomerFilter, ICustomerList, ICustomerEdit
    {
        ICustomerService service = new CustomerService();

        #region ICustomerService
        public ICustomerService CustomerService
        {
            get;
            set;
        }
        #endregion

        #region ICustomerFilter Members
        public string SearchText { get; set; }
        #endregion

        #region IPagingController Members

        private int pageNo = 1;

        public int PageNo
        {
            get { return pageNo; }
            set
            {
                pageNo = value; CustomerList = null;
            }
        }

        private int pageSize = Constants.DefaultGridPageSize;

        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int recordCount = 0;

        public int RecordCount
        {
            get { return recordCount; }
            set { recordCount = value; }
        }

        private string sortOrder;

        public string SortOrder
        {
            get { return sortOrder; }
            set { sortOrder = value; }
        }

        public event EventHandler OnCustomerListChanged = delegate { };

        #endregion

        #region ICustomerList Members
        private IList<Customer> _CustomerList;

        public IList<Customer> CustomerList
        {
            get
            {
                if (_CustomerList == null)
                {
                    var items = string.IsNullOrEmpty(SearchText) ?
                        service.GetAllCustomers() :
                        service.GetSearchCustomers(SearchText);

                    _CustomerList = (from c in items
                                     select c).ToList();

                }
                return _CustomerList;
            }
            set
            {
                _CustomerList = value; OnCustomerListChanged(this, EventArgs.Empty);
            }
        }

        public IList<Customer> SelectCustomerList()
        {
            return CustomerList;
        }

        public void SelectCustomer(string key)
        {
            var items = from item in CustomerList
                        where item.Id == key
                        select item;

            CustomerSelected = items.Count() > 0 ? items.First() : null;
        }

        public void NewCustomer()
        {
            //TODO:
            CustomerSelected = new Customer();
        }

        #endregion

        #region ICustomerEdit Members
        public void Reset()
        {
            CustomerList = null;
            CustomerSelected = null;
        }

        public void UpdateCustomer(Customer Customer)
        {
            //TODO:
            Reset();
        }

        public void DeleteCustomer(Customer Customer)
        {
            //TODO:
            Reset();
        }

        public void InsertCustomer(Customer Customer)
        {
            //TODO:
            Reset();
        }

        public Customer SelectCustomer()
        {
            return CustomerSelected;
        }

        private Customer _CustomerSelected;

        public Customer CustomerSelected
        {
            get { return _CustomerSelected; }
            set { _CustomerSelected = value; OnCustomerChanged(this, EventArgs.Empty); }
        }

        public event EventHandler OnCustomerChanged = delegate { };

        #endregion
    }
            

}
